unit Unit39; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  sqldb, Unit1, DBGrids, Unit37, Grids, Process, lNetComponents, lNet, impresores;

type

  { TForm39 }

  TForm39 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button6: TButton;
    Button7: TButton;
    ComboBox1: TComboBox;
    Edit1: TEdit;
    Edit10: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    GroupBox4: TGroupBox;
    Label1: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    LTCP: TLTCPComponent;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    SQLQuery1: TSQLQuery;
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure LTCPReceive(aSocket: TLSocket);
    procedure nota();
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
  private
    nroitem : longint;
    paso : longint;
    nro_conexion : string;
    archivo_fiscal : string;
    iva : string;
    impint : string;
    items : longint;
  public
    { public declarations }
  end; 

var
  Form39: TForm39; 

implementation

{ TForm39 }

procedure TForm39.nota();
var
  archivo : TextFile;
  proceso : TProcess;

begin
  items := 1;
  nro_conexion := '';
  archivo_fiscal := '';
  paso := 1;

  LTCP.Connect(registro.ip_impresor, puertoTCP);
end;

procedure TForm39.FormCreate(Sender: TObject);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
end;

procedure TForm39.LTCPReceive(aSocket: TLSocket);
var
  mensaje : string;
  auxiliar : string;
  
begin
  if aSocket.GetMessage(mensaje) > 0 then
  begin
    case paso of
      1:
      begin
        nro_conexion := mensaje;
        LTCP.SendMessage('crear_archivo>' + nro_conexion);

        paso += 1;
      end;
      
      2:
      begin
        archivo_fiscal := mensaje;
        
        if (registro.tipo_impresor = 'Hasar PR4F') or
          (registro.tipo_impresor = 'Hasar P715F')then
        begin
          auxiliar := impresor_fiscal.poner_encabezado(Edit2.Text, Edit3.Text, Edit9.Text, Edit4.Text);
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end;
        
        paso += 1;
      end;
      
      3:
      begin
        if ((registro.tipo_impresor = 'Hasar PR4F')
         or (registro.tipo_impresor = 'Hasar P715F')) and (RadioButton1.Checked = true) then
        begin
          auxiliar := impresor_fiscal.poner_nro_factura(Edit1.Text);
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
          LTCP.SendMessage('ping');
          
        paso += 1;
      end;
      
      4:
      begin
        if ((registro.tipo_impresor = 'Hasar PR4F')
         or (registro.tipo_impresor = 'Hasar P715F'))  and (RadioButton2.Checked = true) then
        begin
          if Edit9.Text = 'I' then
            auxiliar := impresor_fiscal.abrir_comprobante('D')
          else
            auxiliar := impresor_fiscal.abrir_comprobante('E');
        end
        else
        if ((registro.tipo_impresor = 'Hasar PR4F')
         or (registro.tipo_impresor = 'Hasar P715F'))  and (RadioButton1.Checked = true) then
        begin
          if Edit9.Text = 'I' then
            auxiliar := impresor_fiscal.abrir_dnfh('R')
          else
            auxiliar := impresor_fiscal.abrir_dnfh('S');
        end;
        
        LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);

        paso += 1;
      end;
      
      5:
      begin
        if (registro.tipo_impresor = 'Hasar PR4F')
          or (registro.tipo_impresor = 'Hasar P715F') then
        begin
          if items <= StringGrid1.RowCount - 1  then
          begin
            auxiliar := impresor_fiscal.agregar_item(StringGrid1.Cells[2, items],
              StrToFloat(StringGrid1.Cells[1, items]), StrToFloat(StringGrid1.Cells[3, items]),
              StrToFloat(StringGrid1.Cells[5, items]), StrToFloat(StringGrid1.Cells[6, items]));
            LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);

            items := items + 1;
          end;
        end;

        if items > StringGrid1.RowCount - 1 then
          paso += 1;
      end;
      
      6:
      begin
        if (registro.tipo_impresor = 'Hasar PR4F')
          or (registro.tipo_impresor = 'Hasar P715F') and (RadioButton1.Checked = true) then
        begin
            auxiliar := impresor_fiscal.cerrar_dnfh();
            LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
        if (registro.tipo_impresor = 'Hasar PR4F')
          or (registro.tipo_impresor = 'Hasar P715F') and (RadioButton2.Checked = true) then
        begin
          auxiliar := impresor_fiscal.subtotal();
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
          LTCP.SendMessage('ping');
          
        paso += 1;
      end;
      
      7:
      begin
        if (registro.tipo_impresor = 'Hasar PR4F')
          or (registro.tipo_impresor = 'Hasar P715F') and (RadioButton2.Checked = true) then
        begin
          auxiliar := impresor_fiscal.cerrar_comprobante();
          LTCP.SendMessage('escribir>' + archivo_fiscal + '>' + auxiliar);
        end
        else
          LTCP.SendMessage('ping');
          
        paso += 1;
      end;
      
      8:
      begin
        LTCP.SendMessage('ejecutar_fiscal>' + archivo_fiscal);

        paso += 1;
      end;

      9:
      begin
        LTCP.SendMessage('borrar_archivo>' +  archivo_fiscal);

        paso += 1;
      end;

      10:
      begin
        LTCP.Disconnect;
      end;
    end;
  end;
  
  if paso = 10 then
    Close;
end;

procedure TForm39.FormShow(Sender: TObject);
var
  vta_cdg : string;
  cliente : string;

begin
  iva := '21.0';
  impint := '0.0';

  StringGrid1.RowCount := 1;
  
  if SQLQuery1.Active = true then
    SQLQuery1.Close;

  nroitem := 1;
  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  Edit4.Text := '';
  Edit5.Text := '';
  Edit6.Text := '';
  Edit7.Text := '';
  Edit8.Text := '0.00';
  Edit9.Text := '';
  Edit10.Text := '';
  
  ComboBox1.Text := 'Lista 1';

  vta_cdg := InputBox('Nota de Crédito / Débito',
    'Ingrese el código de venta (para Nota de Crédito): ', '');

  if vta_cdg <> '' then
  begin
    Edit5.Text := vta_cdg;
    
    SQLQuery1.SQL.Text := 'select vta_cliente from venta where vta_cdg = ' + vta_cdg;
    
    SQLQuery1.Open;
    
    while not SQLQuery1.EOF do
    begin
      cliente := SQLQuery1.Fields[0].AsString;
      SQLQuery1.Next;
    end;
    
    SQLQuery1.Close;

    SQLQuery1.SQL.Text := 'select cln_nombre, cln_cuit, cln_iva, cln_direccion from cliente '
      + 'where cln_cdg = ' + cliente;
      
    SQLQuery1.Open;
    
    while not SQLQuery1.EOF do
    begin
      Edit2.Text := SQLQuery1.Fields[0].AsString;
      Edit3.Text := SQLQuery1.Fields[1].AsString;
      Edit9.Text := SQLQuery1.Fields[2].AsString;
      Edit4.Text := SQLQuery1.Fields[3].AsString;
      SQLQuery1.Next;
    end;
    
    SQLQuery1.Close;
    
    Edit1.SetFocus;
  end
  else
  begin
    Edit1.Enabled := false;
    Edit5.Enabled := false;
    
    Edit2.SetFocus;
  end;
end;

procedure TForm39.RadioButton1Click(Sender: TObject);
begin
  Edit6.SetFocus;
end;

procedure TForm39.RadioButton2Click(Sender: TObject);
begin
  Edit6.SetFocus;
end;

procedure TForm39.Button2Click(Sender: TObject);
begin
  Form37.ShowModal;

  if Form37.codigo_producto <> '' then
  begin
    iva := Form37.iva_producto;
    impint := Form37.impint_producto;
    Edit6.Text := Form37.nombre_producto;
    Edit7.Text := '1';

    if ComboBox1.Text = 'Lista 1' then
      Edit10.Text := FormatFloat('###0.00', StrToFloat(Form37.precio1))
    else
    if ComboBox1.Text = 'Lista 2' then
      Edit10.Text := FormatFloat('###0.00', StrToFloat(Form37.precio2))
    else
    if ComboBox1.Text = 'Lista 3' then
      Edit10.Text := FormatFloat('###0.00', StrToFloat(Form37.precio3))
    else
    if ComboBox1.Text = 'Lista 4' then
      Edit10.Text := FormatFloat('###0.00', StrToFloat(Form37.precio4));

    Edit7.SetFocus;
  end;
end;

procedure TForm39.Button3Click(Sender: TObject);
begin
  StringGrid1.RowCount := StringGrid1.RowCount + 1;
  
  StringGrid1.Cells[1, nroitem] := FormatFloat('###0.00', StrToFloat(Edit7.Text));
  StringGrid1.Cells[2, nroitem] := Edit6.Text;
  StringGrid1.Cells[3, nroitem] := Edit10.Text;
  StringGrid1.Cells[4, nroitem] := FormatFloat('###0.00',
    StrToFloat(Edit7.Text) * StrToFloat(Edit10.Text));
  StringGrid1.Cells[5, nroitem] := iva;
  StringGrid1.Cells[6, nroitem] := impint;
  
  Edit8.Text := FormatFloat('###0.00',
    StrToFloat(Edit8.Text) + StrToFloat(StringGrid1.Cells[4, nroitem]));

  nroitem += 1;

  Edit6.Text := '';
  Edit7.Text := '';
  Edit10.Text := '';
  
  iva := '21.0';
  impint := '0.0';
  
  Edit6.SetFocus;
end;

procedure TForm39.Button4Click(Sender: TObject);
begin
  StringGrid1.RowCount := StringGrid1.RowCount - 1;
  nroitem -= 1;
  
  iva := '21.0';
end;

procedure TForm39.Button6Click(Sender: TObject);
begin
  nota();
end;

procedure TForm39.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;
end;

initialization
  {$I unit39.lrs}

end.

